package com.example.demo.heartbeat;

import com.alibaba.fastjson.JSONObject;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

public class HeartBeatDecoder extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        byte[] bytes = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(bytes);
        String content = new String(bytes);
        JSONObject object = JSONObject.parseObject(content);
        HeartBeat heartBeat = new HeartBeat();
        heartBeat.setBusinessId(object.getLong("businessId"));
        heartBeat.setHostName(object.getString("hostName"));
        heartBeat.setHostAddress(object.getString("hostAddress"));
        heartBeat.setClientVersion(object.getString("clientVersion"));
        list.add(heartBeat);
    }
}
